package servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.CheckUsernameByAjaxService;

/**
 * 模糊查询
 */
public class SearchkwServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//从前端获取数据
		String kw=request.getParameter("kw");
		
		List list=null;
		//调用service的方法，返回一个list集合
		try {
			list=new CheckUsernameByAjaxService().searchkw(kw);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		//将数据去掉括号些回去？？，将数据【a,aa,aaa】去掉括号些回去
		if(list!=null && list.size()>0){
			String s=list.toString();  //数组变成字符串
			s=s.substring(1,s.length()-1);   //这个是去掉括号
			System.out.println(s);
			response.getWriter().println(s);    //能从页面上展示出来
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
